<?php
class paymentclass {

	function paymentclass() {
		global $setupdata, $mysqlclass;
		$this->setupdata=$setupdata;
		$this->mysqlclass=$mysqlclass;
	}

	function amount_convert($price, $curr){
		global $setupdata;
		if ($setupdata['vars']['curency']['default'] != $curr){
			global $shopclass;
			$curency = $shopclass->get_curency();
			if (is_array($curency)) {
				foreach ($curency as $key => $value) {
					if ($curr == $key) $price = round($price / $value, 2);
				}
			}
		}
		return $price;
	}
	
	function get_gateway_data($gateway){
		$sql = $this->mysqlclass->mysql_my_query("SELECT `value` FROM `".$this->setupdata['table_prefix']."local_vars` WHERE 'payment_gateway_".$gateway."'=`name`", __LINE__, __FILE__);
		if (mysql_num_rows($sql) == 1) {
			$row = mysql_fetch_array($sql, MYSQL_ASSOC);
			$result = unserialize($row['value']);
			if (is_array($result)){
				foreach($result as $key=>$value) $result[$key] = stripslashes($value);
				return $result;
			} else return array();
		} else return array();
	}
	
	function sent_order_to_gateway($gateway, $data, $post=""){
		if ($post == "") $post = $_POST;
		global $err;
		$res = $this->save_order_to_db($gateway, $data);
		if ($res > 0){
			$data['VK_STAMP'] = $res;
			$class = $this->setupdata['core_dir']."/modules/payment/gateway/".$gateway."class.php";
			if (file_exists($class)) {
				include $class;
				$var = $gateway."class"; eval("\$$var = new $var;"); 
				$gateway_data = $this->get_gateway_data($gateway);
				$$var->send_order_to_gateway($gateway_data, $data, $res, $post);
			} else {
				$err->AddError(29, "payment");
			}
		} else $err->AddError(29, "payment");
	}
	
	function save_order_to_db($gateway, $data){
		$data['mess'] = trim($data['mess']); $data['mess'] = addslashes($data['mess']);
		if (isset($data['order_id'])) $data['mess'] .= " (".$data['order_id'].")";
		$data['order_from'] = $data['order_from']."||".$data['order_id'];
		$sql = $this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."payments` (`id`,`name`,`date`,`gateway`,`from`,`amount`,`bank_status`,`status`,`ip`) VALUES ('', '".$data['mess']."', '".date("YmdHis")."', '".$gateway."', '".$data['order_from']."', ".$data['order_amount'].", 'wait', 'wait', '".$_SERVER['REMOTE_ADDR']."')", __LINE__, __FILE__);
		if ($sql) return mysql_insert_id();
		else return 0;
	}
	
	function get_invoice($from, $to, $in_page, $gateway){
		$result = array();
		$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."payments` ORDER BY `date` DESC", __LINE__, __FILE__);
		while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)) {
			$row['name'] = stripslashes($row['name']);
			$result[$row['id']] = $row;
		}
		return $result;
	}
	
	function set_admin_invoice_status($invoice="0", $status='wait'){
		if ((($status=='wait')||($status=='pay')||($status=='notpay'))&&(intval($invoice) > 0)){
			$this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."payments` SET `status`='".$status."' WHERE `id`=".$invoice, __LINE__, __FILE__);
		}
	}
	
	function set_invoice_status($invoice="0", $status='wait'){
		if ((($status=='wait')||($status=='pay')||($status=='notpay')||($status=='error'))&&(intval($invoice) > 0)){
			$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."payments` WHERE ".$invoice."=`id`", __LINE__, __FILE__);
			if (mysql_num_rows($sql) > 0) $result = mysql_fetch_array($sql, MYSQL_ASSOC);
			if ($result['status'] == 'wait'){
				$this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."payments` SET `bank_status`='".$status."' WHERE `id`=".$invoice, __LINE__, __FILE__);
			}
		}
	}
	
	function get_invoice_data($invoice, $gateway){
		$result = array();
		$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."payments` WHERE ".$invoice."=`id` AND `gateway`='".$gateway."'", __LINE__, __FILE__);
		if (mysql_num_rows($sql) > 0) $result = mysql_fetch_array($sql, MYSQL_ASSOC);
		return $result;
	}
	
	function del_invoice($array){
		foreach($array as $key=>$value){
			$value = intval($value);
			if ($value > 0) $this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."payments` WHERE ".$value."=`id`", __LINE__, __FILE__);
		}
		return 0;
	}
}
?>
